<?php
die;
class admin extends common
{
	static function config(){//后台整体配置
		$cache=spAccess('r',$cache);
		$cache=!empty($cache)&&$cache!=false ?array('cache'=>$cache):array();
		$lang= isset($_SESSION['lang'])?$_SESSION['lang']:'ch';
		$config=array(
			'site'  =>'网站后台管理系统',
			'verify'=>true,//是否开启验证码
			'repass'=>false,//是否开启密码找回
			'backup'=>true,
			'mail'=>false,
			'page'=>15,
			'copyright'=>'网站版权',
			'lang'=>$lang,
			);
		return $config=array_merge($config,$cache);
		}	
	private function setCacheTime($value){	
		spAccess('w',$cache, $value, $life_time = -1);
	}
	private function chkpass($pass,$sigPassword){
		import(APP_PATH.'/controller/lib/class-phpass.php');
		$hasher = new PasswordHash(8, TRUE);
		$Login=$hasher->CheckPassword($pass,$sigPassword);
		return $Login;
	}
	private function passgen($pass){
		import(APP_PATH.'/controller/lib/class-phpass.php');
		$passwordValue = $pass;  
		$hasher = new PasswordHash(8, TRUE);  
		return $hasher->HashPassword($passwordValue);
	}	
	private function chkLog(){//简单判断是否登录
		if(isset($_SESSION['admin'])){
			return true;
		}else{
			return false;
		}		
		}
	private function DataToSave($saveMapArr,$arraytemp,$newrow=array()){
		foreach($saveMapArr as $id=>$v){
			$tempArr=array($v=>$arraytemp[$id]);
			$newrow=array_merge($newrow,$tempArr);
		}
			unset($saveMapArr,$arraytemp,$tempArr);
		return $newrow;	
		}		
	function __construct(){
		parent::__construct();
		define("CURRENT_PATH",dirname(__FILE__));		
		require(CURRENT_PATH.'/lib/lib_common.php');
	    spAddViewFunction('insert_scripts','smarty_insert_scripts');
if($this->chkLog())	{	
				$config=admin::config();
				$this->site=$config[site];
				$this->copyright=$config[copyright];
				$this->postLink=$_SERVER[REQUEST_URI].'&act=post';
				$a=$_REQUEST['a'];
				$leftarray = array('welcome','sysconfig','article','message','products','others','seo');
				$mod=$_GET['mod'];
				if(in_array($a,$leftarray)){
					if(!isset($_GET['mod'])){
						$lang=$config[lang];
						require(CURRENT_PATH.'/include/language/lang_'.$lang.'_admincp_left.php');
						require(CURRENT_PATH.'/include/left_menu.php');
						$this->menu=$menu[$a];
						$this->display('management/left2.html');	
					}else{
						//权限管理 todo
						$authTemp=unserialize($_SESSION['authority']);
						$authArrayMap=array(
							0=>'sysconfig',
							1=>'products',
							2=>'article',
							3=>'message',
							4=>'others',
						);
						$authVerify=array('seo','welcome',);
						//dump($authTemp);
						foreach($authTemp as $authid => $v){
							if($authTemp[$authid]==1){
							   $authVerify=array_merge($authVerify,array($authArrayMap[$authid]));
								}
							}
						if(in_array($a,$authVerify)){
							require(CURRENT_PATH.'/include/'.$a.'/'.$mod.'.php');							
						}else{
							$this->error("您没有权限操作此内容，请联系系统管理员");
							}
					}
				}
		}//待完善
	}
	function getpass(){
	$config=admin::config();
	if($config['repass']){		
		}else{
	$this->error("尚未开启密码找回功能",spUrl("admin","index"));
		}	
		}
    private function login(){
		$config=admin::config();
		$this->config=$config;
		$this->display('admin/login.html');
    }
	function logout(){
		session_unset();//释放内存session变量,不删除session_id  
		session_destroy();//删除session文件与session_id 
		$this->jump(spUrl('admin', 'index')); 
		}
	private function guide($msg,$url){
		$this->msg=$msg;
		$this->url=$url;
		$this->display('management/guide.html');		
		}	
	function logAction(){
		//dump($this->spArgs());
		$config=admin::config();
		if($config['verify']){//检查验证码配置；
			$verifyCondition=md5(strtoupper($this->spArgs(captcha)))==$_SESSION['verify'];//将用户输入验证码转换为大写
		}else{
			$verifyCondition=1;
		}	
		$verifyCondition;
		if($verifyCondition){
		$LogIn=spClass(manage);
		$P=null;
		$P=$LogIn->find($conditions=array('username'=>$this->spArgs(username)),'',$fields=null);
		$sigPassword=$P[password];
		if($this->chkpass($this->spArgs(password),$sigPassword)){
				$Login=$P;
				unset($P);
				unset($_SESSION['verify']);
				$_SESSION['adminUserName'] = $Login['username'];
				$_SESSION['admin']         = true;
				$_SESSION['authority']     = $Login[auth];	//管理员权限序列化后的数组
				$_SESSION['id']            = $Login[id];	//用户ID
				//$this->success('恭喜，登录成功！', spUrl('admin', 'index'));
				$this->guide('恭喜登录成功，正在为您跳转至系统管理页',spUrl('admin', 'index')); 
			}else{
				
				
				
				$this->error("登陆失败，请仔细检查您的用户名和密码！",spUrl("admin","index"));					
				}
			}else{
				//$this->error("验证码输入有误，请勿禁用浏览器javascript！",spUrl("admin","index"));
				$this->jump(spUrl("admin","index"));			
			}		
		}
     function index(){
		if($this->chkLog()){
			$this->display('admin/mainframe.html');//已登录，显示首页			
		}else{
			$this->login();//未登录
		}      
    }
    function top(){
		if($this->chkLog()){
			$config=$this->config();
			$lang=$config['lang'];
			require(CURRENT_PATH.'/include/language/lang_'.$lang.'_admincp_top.php');
			$this->Lang=$lang_top;
        	$this->display('management/top.html');
		}
    }
    function main(){
		if($this->chkLog()){
			echo $temp;
			//include('probe.php');
			echo phpinfo();
        	//$this->display('management/main.html');
		}
    }
    function drag(){
        $this->display('management/drag.html');
    }   
    function left(){
    } 
	function clear_cache(){
//	$type=empty($_REQUEST['type']) ? 0 : $_REQUEST['type'];
		$cache=spAccess('r',$cache);//读取缓存时间
		spClass("spCache")->clear_all();
		$smarty = $this->getView();
		$smarty->clearCompiledTemplate(); 
		spAccess('w',  $cache, $value =3600*24, $life_time = -1);//重建缓存时间
		header("content-type:text/html; charset=utf-8");
	echo'<script>alert(\'所有缓存清理成功\');window.history.go(-1)</script>';
		}
function langswitch(){
	$lang=isset($_SESSION["Lang"])&&($_SESSION["Lang"]==1||$_SESSION["Lang"]==0)?$_SESSION["Lang"]:1;
	if($lang==1){$_SESSION["Lang"]=0;}else if($lang==0){$_SESSION["Lang"]=1;}
	echo "<script>window.top.frames['header-frame'].document.location.reload()</script>";
}		
function sysconfig(){		
		}
function welcome(){
}
function products(){
}
function article(){
}
function message(){
}
function others(){
}
function seo(){		
}	
private function totalprice($array){
	$priceMap=array(
		1=>array(),
		2=>array(),
		3=>array(),
		4=>array(),
		5=>array(),
	);
	
//to do

//dump($array);

//	
	
return $price_total=1000;	
	}	
}